package UIFree.controls.popupMenu
{
	import UIFree.ObjectEvent;
	import UIFree.components.uif_shortKeyManager;
	import UIFree.controls.uif_gbutton;
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	
	import UIUtils.uif_util;
	
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextFormat;
	
	public class uif_menuItem extends uif_gbutton {
		private var _shortKeys: uif_label;
		public function uif_menuItem(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:Object='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
		}
		
		override protected function setData(value: Object): void {
			super.setData(value);
			if (value.shortkeys) setShortKeys(value.shortkeys);
			else if (value.submenu) shapeArrow();
		}
		
		public function setShortKeys(a_value: String): void {
			var inner: Rectangle = getInner();
			var tf: TextFormat = textField.getTextFormat();
			var a_style: Object = uif_util.union({
				text_format: tf
			}, app.styleByObj(style.shortKeys));
			var size: Point = getTextSize(a_value, a_style.text_format.font, int(a_style.text_format.size));
			_shortKeys = new uif_label(this, inner.x + inner.width - size.x, inner.y, size.x, height, style.shortKeys);
			
			_shortKeys.text = a_value;
			
			dispatchEvent(new ObjectEvent(uif_shortKeyManager.ADDSHORTKEY, a_value, true, true));
			addEvent(uif_shortKeyManager.SHORTKEYDOWN, doShortKeyDown);
		}
		
		protected function shapeArrow(): void {
			var inner	: Rectangle = getInner();
			var arrow	: Shape = new Shape();
			var size	: Number = inner.height * 0.3;
			arrow.graphics.beginFill(0);
			arrow.graphics.lineTo(size, size);
			arrow.graphics.lineTo(0, size * 2);
			arrow.graphics.endFill();
			arrow.y = inner.y + (inner.height - size * 2) / 2;
			arrow.x = inner.x + inner.width - size;
			addChild(arrow);
		}
		
		protected function doShortKeyDown(e: Event): void {
			dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		}
	}
}